/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.Product;

/**
 *
 * @author pelle
 */
public class ProductDb extends AbstractDb implements ProductInterface {

    public Product getProduct(int productId) {
        Product product = null;

        try {
            ResultSet rs = this.db.query(""
                + "SELECT * "
                + "FROM Product "
                + "WHERE product_id = " + productId);

            if (rs != null && rs.next()) {
                product = new Product(
                    rs.getInt("product_id"),
                    rs.getString("product_name"),
                    rs.getFloat("product_purchasePrice"),
                    rs.getFloat("product_salesPrice"),
                    rs.getFloat("product_rentPrice"),
                    rs.getString("product_country"),
                    rs.getInt("product_minstock"));
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductDb.class.getName()).log(Level.SEVERE, null, ex);
        }

        return product;
    }

    public ArrayList<Product> searchProducts(String searchString) {
        ArrayList<Product> products = new ArrayList<Product>();
        if(searchString.length() > 0) {
            try {
                ResultSet items = this.db.query(""
                    + "SELECT * "
                    + "FROM Product "
                    + "WHERE product_name LIKE '%" + searchString + "%'");

                if (items != null) {
                    while(items.next()) {
                        Product product = new Product(
                                items.getInt("product_id"),
                                items.getString("product_name"),
                                items.getFloat("product_purchasePrice"),
                                items.getFloat("product_salesPrice"),
                                items.getFloat("product_rentPrice"),
                                items.getString("product_country"),
                                items.getInt("product_minstock"));

                        products.add(product);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(OrderDb.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return products;
    }
    
}
